<template>
  <section>
      <div class="filter-container">
            <el-button size="mini" class="ml10" @click="handleCreate" type="primary" icon="el-icon-plus">新增</el-button>
        </div>
        <el-table
                :data="list"
                style="width: 100%">
            <el-table-column align="left" label="充值金额">
                <template slot-scope="scope">
                    <span>{{scope.row.price}}</span>
                </template>
            </el-table-column>
            <el-table-column align="left" label="销售金额">
                <template slot-scope="scope">
                    <span>{{scope.row.salePrice}}</span>
                </template>
            </el-table-column>
            <el-table-column align="left" label="折扣">
                <template slot-scope="scope">
                    <span>{{scope.row.rebate}}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column align="center" label="排序">
                <template slot-scope="scope">
                    <span>{{scope.row.orderNum}}</span>
                </template>
            </el-table-column> -->
            <el-table-column
                    fixed="right"
                    label="操作"
                    width="100">
                <template slot-scope="scope">
                    <el-dropdown>
                        <el-button type="text">操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="handleUpdate(scope.row)">编辑</el-dropdown-item>
                            <el-dropdown-item @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
        <!-- <div v-show="!listLoading" class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[20,30, 50]" :page-size="listQuery.num" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
        </div> -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="500px" @close="dialogClose">
            <el-form :model="form" :rules="rules" ref="form" label-width="150px">
                <el-form-item label="充值金额" prop="price">
                    <el-input size="mini" type="number" v-model="form.price" placeholder="" maxlength="10" />
                </el-form-item>
                <el-form-item label="销售金额" prop="salePrice">
                    <el-input size="mini" type="number" v-model="form.salePrice" placeholder="" maxlength="10" />
                </el-form-item>
                <!-- <el-form-item label="排序" prop="orderNum">
                    <common-input v-model="form.orderNum" class="w60" maxlength="3" />
                </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click="cancel('form')">取消</el-button>
                <el-button size="mini" v-if="dialogStatus=='create'" type="primary" @click="create('form')">确定</el-button>
                <el-button size="mini" v-else type="primary" @click="update('form')">确定</el-button>
            </div>
        </el-dialog>
  </section>
</template>

<script>
    import {
        isIntegerNotMust
    } from '../../utils/validator'
    import {
        recharge_package_list,
        recharge_package_save,
        recharge_package_del
    } from '../../api/recharge';
export default {
    components: {
    },
  data() {
    return {
        list: null,
        listLoading: true,
        listQuery: {
            keyword: undefined,
        },
        total: null,
        dialogStatus: '',
        dialogFormVisible: false,
        textMap: {
            update: '编辑套餐',
            create: '新增套餐'
        },
        form: {
            id: undefined,
            price: undefined,
            salePrice: undefined,
            orderNum: 0,
            status:0
        },
        rules: {
            price: [
                {
                    required: true,
                    message: '请输入充值金额',
                    trigger: 'blur'
                }
            ],
            salePrice: [
                {
                    required: true,
                    message: '请输入销售金额',
                    trigger: 'blur'
                }
            ],
            // orderNum: [
            //     {
            //         validator: isIntegerNotMust,
            //         trigger: 'blur'
            //     }
            // ]
        },
    }
  },
  mounted() {
  },
  created() {
    this.getList();
  },

  methods: {
    getList() {
        this.listLoading = true;
        recharge_package_list(this.listQuery).then(response => {
            this.list = response.data;
            this.listLoading = false;
        })
    },
    handleCreate() {
        this.resetTemp();
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
    },
    resetTemp() {
        this.form = {
            id: undefined,
            price: undefined,
            salePrice: undefined,
            orderNum: 0,
        };
    },
    create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
            if (valid) {
                if(!this.form.orderNum) {
                    this.form.orderNum = 0;
                }
                recharge_package_save(this.form)
                    .then(() => {
                        this.dialogFormVisible = false;
                        this.getList();
                        this.$message.success("套餐创建成功");
                    })
            } else {
                return false;
            }
        });
    },
    update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
            if (valid) {
                if(!this.form.orderNum) {
                    this.form.orderNum = 0;
                }
                this.dialogFormVisible = false;
                this.form.password = undefined;
                recharge_package_save(this.form).then(() => {
                    this.dialogFormVisible = false;
                    this.getList();
                    this.$message.success("套餐更新成功");
                });
            } else {
                return false;
            }
        });
    },
    cancel(formName) {
        this.dialogFormVisible = false;
        this.$refs[formName].resetFields();
    },
    handleUpdate(row) {
        this.form = row;
        this.dialogFormVisible = true;
        this.dialogStatus = 'update';
    },
    handleDelete(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        })
            .then(() => {
                recharge_package_del(row.id)
                    .then(() => {
                        this.$message.success("套餐删除成功");
                        const index = this.list.indexOf(row);
                        this.list.splice(index, 1);
                    });
            }).catch(()=>{});
    },
    dialogClose() {
        this.$refs.form.resetFields();
    },
    handleFilter() {
        this.getList();
    },
    handleSizeChange(val) {
        this.listQuery.num = val;
        this.getList();
    },
    handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
    }
  }
};
</script>
